<template>
	<view class="module flex_ZC">
		<NavTitle :title="title" :backgroundColor='backgroundColor' @leftClick="leftClick"></NavTitle>
		<view class="information_txt flex_Z">
			<text class="f48 c3 fw7">上传身份信息</text>
		</view>

		<view class="content padb30 flex_Z">
			<view class="  flex_Z">
				<view class="flex_dq">
					<text class="f28 c3 fw5">上传身份证</text>
					<text class="tishi"> *</text>
				</view>
				<text class="content_tip">上传营业执照上经营者/法定代表人身份证</text>
			</view>
			<u-upload :fileList="fileList1" @afterRead="afterRead($event,'face')" @delete="deletePic($event,'face')"
				name="1" :maxCount="1" :maxSize="6 *1024 *1024" @oversize="overSize" width="320" height="190">
				<view class="content_r flex_c">
					<image :src="baseUrl +'/static/image/sfzzm.png'" mode="aspectFit"></image>
				</view>
			</u-upload>

			<u-upload :fileList="fileList2" @afterRead="afterRead($event,'back')" @delete="deletePic($event,'back')"
				name="2" :maxCount="1" :maxSize="6 *1024 *1024" @oversize="overSize" width="320" height="190">
				<view class="content_r mart20 flex_c">
					<image :src="baseUrl +'/static/image/sfzfm.png'" mode="aspectFit"></image>
				</view>
			</u-upload>
		</view>

		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">姓名</text>
				<text class="tishi"> *</text>
			</view>
			<u--input placeholder="请输入姓名" fontSize="14" color="#666666" border="none "
				v-model.trim="ocrIdentityInfo.id_card_name" clearable></u--input>
		</view>
		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">身份证号</text>
				<text class="tishi"> *</text>
			</view>
			<u--input placeholder="请输入身份证号 " fontSize="14" color="#666666" border="none "
				v-model.trim="ocrIdentityInfo.id_number" clearable></u--input>
		</view>
		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">身份证有效期</text>
				<text class="tishi"> *</text>
			</view>
			<view class="padb30 mart20 flex_dq">
				<text class="f28 c6">{{ocrIdentityInfo.id_number_start}}</text>
				<text class="f28 c6 mlr"> 至</text>
				<text class="f28 c6">{{ocrIdentityInfo.id_number_end}}</text>
			</view>
		</view>
		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">所在地址</text>
				<text class="tishi"> *</text>
			</view>
			<u--input placeholder="请输入所在地址" fontSize="14" color="#666666" border="none "
				v-model.trim="ocrIdentityInfo.id_address" clearable></u--input>
		</view>
		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">法人手机号</text>
				<text class="tishi"> *</text>
			</view>
			<view class="describe">
				<image :src="baseUrl + '/static/ymimage/remind.png'" class="remind" mode="aspectFill"></image>
				<text class="">必须为法人实名认证的手机号码</text>
			</view>
			<u--input placeholder="请输入法人手机号"  type="number" fontSize="14" maxlength="11" color="#666666" border="none "
				v-model.trim="ocrIdentityInfo.legal_mobile_no" clearable></u--input>
		</view>

		<view class="btns flex_ld">
			<text class="sign_out" @click="leftClick">上一步</text>
			<text class="service" @click="$u.debounce(nextStep, 300)">下一步</text>
		</view>

		<view class="empty3"></view>

	</view>
</template>

<script>
		import UploadFile from '../../common/upload'
	import {
		baseURL
	} from "../../utils/request.js"

	import { settledThreeSave } from '@/api/settled.js'
	export default {
		data() {
			return {
				title:"上传法人身份信息",
				backgroundColor: '',
				baseUrl: this.$imageBaseUrl,
				fileList1: [],
				fileList2: [],
				halfSfzzm: '', //半路经正面
				halfSfzfm: '', //半路经反面
				firstShow: false,
				modifyState: false, //是否通过修改按钮进入
				
				ocrIdentityInfo: {
					id_card_name: '',
					id_number: '',
					id_number_start: '',
					id_number_end: '',
					id_address:'',
					legal_mobile_no:''
				}, //ocr识别信息
				seller_id:""

			}
		},
	
		onLoad(options) {
			if(options.data){
				let data = JSON.parse(options.data)
				this.seller_id = data.id
				if(data.modifyState){
					this.modifyState = data.modifyState
					this.ymDetails = JSON.parse(uni.getStorageSync('ymShopDetails')) || {}
					let seller_auth = this.ymDetails.seller_auth
					this.halfSfzzm = this.getPathAfterCom(seller_auth.id_card_front)
					this.halfSfzfm = this.getPathAfterCom(seller_auth.id_card_back)
					const aaa = {
						id_card_name: seller_auth.id_card_name,
						id_number: seller_auth.id_number,
						id_number_start: seller_auth.id_number_start,
						id_number_end: seller_auth.id_number_end,
						id_address:seller_auth.id_address,
						legal_mobile_no:seller_auth.legal_mobile_no,
					}
					this.ocrIdentityInfo = aaa;
					this.fileList1.push({
						'thumb':seller_auth.id_card_front,
						'status': 'success'
					})
					this.fileList2.push({
						'thumb':seller_auth.id_card_back,
						'status': 'success'
					})
				}
				
			}
			if(options.id){
				this.seller_id = options.id
			}
		},
		onShow() {
			// if (uni.getStorageSync('ocrIdentityInfo')) {
			// 	this.ocrIdentityInfo = uni.getStorageSync('ocrIdentityInfo')
			// }
		},
		methods: {
			leftClick() {
				//获取页面栈的长度
				const canNavBack = getCurrentPages()
				console.log(canNavBack.length);
				// 判断是否刷新了浏览器，刷新了浏览器，页面栈只有当前一个
				if (canNavBack.length > 1) {
					uni.navigateBack({
						delta: 1
					})
				} else {
					uni.switchTab({
						url: '/pages/index/cloudCatIndex'
					})
				}
			},

			// 截取半路经
			getPathAfterCom(url) {
				const parts = url.split('.com');
				return parts.length > 1 ? parts[1] : '';
			},
			
			// 超出提示
			overSize(event){
				// console.log(event);
				uni.$u.toast('图片过大,请重新上传!')
			},
			// 删除图片
			deletePic(event, side) {
				console.log(event, side);
				this[`fileList${event.name}`].splice(event.index, 1);
				if (side == 'face') {
					this.halfSfzzm = ''
				} else {
					this.halfSfzfm = ''
				}
			},
			// 新增图片
			async afterRead(event, side) {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				const upload = new UploadFile('face',true)
				await upload.afterRead(event).then(res=>{
					this[`fileList${event.name}`] = res.filelist
					if(side == 'face'){
						this.halfSfzzm = res.halfRoadRoute[0]
						this.ocrIdentityInfo.id_card_name = res.ocrData.name
						this.ocrIdentityInfo.id_number = res.ocrData.idcard
						this.ocrIdentityInfo.id_address = res.ocrData.address
					}else{
						this.halfSfzfm = res.halfRoadRoute[0]
						this.ocrIdentityInfo.id_number_start = res.ocrData.start_date
						this.ocrIdentityInfo.id_number_end = res.ocrData.end_date
					}
					uni.hideLoading();
				})
			},
			
			
			// 规则校验
			ruleValidation() {

				if (this.fileList1.length <= 0) {
					uni.$u.toast('请上传身份证正面');
					return false;
				}
				if (this.fileList2.length <= 0) {
					uni.$u.toast('请上传身份证反面');
					return false;
				}
				if (uni.$u.test.isEmpty(this.ocrIdentityInfo.id_card_name)) {
					uni.$u.toast('请输入法人姓名');
					return false;
				}
				if (uni.$u.test.isEmpty(this.ocrIdentityInfo.id_number)) {
					uni.$u.toast('请输入身份证号');
					return false;
				}
				if (uni.$u.test.isEmpty(this.ocrIdentityInfo.id_address)) {
					uni.$u.toast('请输入所在地址');
					return false;
				}
				if (!uni.$u.test.mobile(this.ocrIdentityInfo.legal_mobile_no)) {
					uni.$u.toast('请输入法人手机号');
					return false;
				}
				return true;
			},

			// 下一步
			nextStep() {
				
				if (this.ruleValidation()) {
					uni.showLoading({
						title: '',
						mask: true
					});
					const data = {
						seller_id:this.seller_id,
						id_card_front:this.halfSfzzm,
						id_card_back:this.halfSfzfm,
						...this.ocrIdentityInfo,
						mask:1
					}
					settledThreeSave(data).then(res=>{
						console.log(res);
						if(res.code == 1){
							const data = {
								id:this.seller_id
							}
							if(this.modifyState){
								data.modifyState = true
							}
							uni.navigateTo({
								url: '/pageYm/ymJoin/joinApplyFour?data=' + JSON.stringify(data),
								success() {
									uni.hideLoading()
								}
							})
						}
					})
					
				}
			},
			// 确认修改
			confirmModify() {
				if (this.ruleValidation()) {
					this.$set(this.ocrIdentityInfo, "id_card_front", this.halfSfzzm);
					this.$set(this.ocrIdentityInfo, "id_card_back", this.halfSfzfm);
					console.log(this.ocrIdentityInfo);
					uni.setStorageSync('ocrIdentityInfo', this.ocrIdentityInfo)
					uni.setStorageSync('sfzShow', true)
					uni.navigateBack({
						delta: 1
					})
				}
			}
		},
		// 页面滚动
		onPageScroll: function(e) {
			const scrollTop = e.scrollTop; // 获取页面滚动位置
			// 根据滚动位置修改状态栏样式
			if (scrollTop > 20) {
				this.backgroundColor = '#ffffff';
			} else {
				this.backgroundColor = '';
			}
			// console.log("滚动距离为：" + e.scrollTop);
		},
	}
</script>

<style scoped>
	.module {
		width: 750rpx;
		margin: 0 auto;
		padding: 0 32rpx;
	}

.describe {
		background: rgba(250, 110, 1, 0.1);
		border-radius: 10rpx;
		font-size: 22rpx;
		color: #FA6E01;
		margin: 24rpx 0;
		padding: 8rpx 16rpx;
		display: flex;
		align-items: center;
	}

	.remind {
		width: 22rpx;
		height: 22rpx;
		margin-right: 8rpx;
	}
	.information_txt {
		width: 686rpx;
		padding: 16rpx 0;
	}


	.content {
		width: 686rpx;
		box-shadow: inset 0rpx -1rpx 0rpx 0rpx rgba(0, 0, 0, 0.05);
		margin: 30rpx 0 10rpx;
	}

	.content_tip {
		font-size: 20rpx;
		color: #999999;
		margin: 10rpx 0 20rpx;
	}

	.content_r {
		width: 690rpx;
		height: 390rpx;
		border: 1rpx solid rgba(0, 0, 0, 0.1);
		border-radius: 20rpx;
		margin: 0 auto;
	}

	.content_r>image {
		width: 588rpx;
		height: 360rpx;
	}

	.mart20 {
		margin-top: 20rpx;
	}

	.padb30 {
		padding-bottom: 30rpx;
	}

	.tishi {
		font-size: 28rpx;
		color: #FA6E01;
		margin-left: 6rpx;
	}

	.mlr {
		margin: 0 10rpx;
	}


	.btns {
		position: fixed;
		bottom: 0rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 690rpx;
		height: 150rpx;
		background: #f8f8f8;
		z-index: 10;
	}

	.sign_out {
		width: 158rpx;
		height: 88rpx;
		font-size: 32rpx;
		color: #333333;
		line-height: 88rpx;
		text-align: center;
		background: #FFFFFF;
		font-weight: 500;
		border-radius: 20rpx;
		border: 2rpx solid rgba(153, 153, 153, 0.18);
	}

	.service {
		width: 504rpx;
		height: 88rpx;
		line-height: 88rpx;
		font-size: 32rpx;
		color: #333333;
		font-weight: 500;
		text-align: center;
		background: #FFCC28;
		border-radius: 20rpx;
	}

	.service2 {
		width: 690rpx;
		height: 88rpx;
		line-height: 88rpx;
		font-size: 32rpx;
		color: #333333;
		font-weight: 500;
		text-align: center;
		background: #FFCC28;
		border-radius: 20rpx;
	}
	/deep/.u-upload__wrap{
		margin: 0 auto !important;
	}
	/deep/.u-input__content {
		margin-left: -20rpx;
	}

	/deep/.u-upload__deletable {
		height: 24px !important;
		width: 24px !important;
	}

	/deep/.u-icon__icon {
		font-size: 30rpx !important;
		line-height: 30rpx !important;
	}
	/* #ifdef MP-ALIPAY */
	/deep/.u-input__content__field-wrapper__field{
		background: transparent!important;
	}
	/* #endif */
</style>